<?php

/**
 * Behavior para ajustar os campos float
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @author        Juan Basso <jrbasso@gmail.com>
 * @author        Daniel Pakuschewski <contato@danielpk.com.br>
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * AjusteFloatBehavior
 *
 * @link http://wiki.github.com/jrbasso/cake_ptbr/behavior-ajustefloat
 */
class AjusteFloatBehavior extends ModelBehavior {

    /**
     * Campos do tipo float
     *
     * @var array
     * @access public
     */
    public $floatFields = array();

    /**
     * Setup
     *
     * @param Model $model
     * @param array $config
     * @return void
     * @access public
     */
    public function setup(Model $model, $config = array()) {
        $this->floatFields[$model->alias] = array();
        foreach ($model->schema() as $field => $spec) {
            if (isset($spec['type']) AND $spec['type'] == 'float') {
                $this->floatFields[$model->alias][] = $field;
            }
        }
    }

    /**
     * Before Find
     * Transforma o valor de BRL para o formato SQL antes de executar uma query
     * com conditions.
     * 
     * @param object $model
     * @return boolean
     * @access public
     */
    public function beforeValidate(Model $model, $options = array()) {
        foreach ($model->data[$model->alias] as $field => $value) {
            if (isset($model->_schema[$field]) AND $model->_schema[$field]['type'] == 'float') {
                $model->data[$model->alias][$field] = $this->_corrigirValor($value);
            }
        }
        return true;
    }

    /**
     * Before Find
     * Transforma o valor de BRL para o formato SQL antes de executar uma query
     * com conditions.
     * 
     * @param object $model
     * @return array
     * @access public
     */
    public function beforeFind(Model $model, $query) {
        if (is_array($query['conditions'])) {
            foreach ($query['conditions'] as $field => $value) {
                if (strpos($field, '.') === false) {
                    $field = $model->alias . '.' . $field;
                }
                list($modelName, $field) = explode('.', $field);
                $modelName = str_replace('`', '', $modelName);
                $useModel = ($modelName != $model->alias) ? $model->{$modelName} : $model;
                if (isset($useModel->_schema[$field]['type']) AND$useModel->_schema[$field]['type'] == 'float') {
                    $value = $this->_corrigirValor($value);
                    if (isset($query['conditions'][$field])) {
                        $query['conditions'][$field] = $value;
                    }
                    if (isset($query['conditions'][$useModel->alias . '.' . $field])) {
                        $query['conditions'][$useModel->alias . '.' . $field] = $value;
                    }
                }
            }
        }
        return $query;
    }

    /**
     * Before Save
     *
     * @param object $model
     * @return void
     * @access public
     */
    public function beforeSave(Model $model, $options = array()) {
        foreach ($model->data[$model->alias] as $field => $value) {
            if (isset($model->_schema[$field]['type']) AND $model->_schema[$field]['type'] == 'float') {
                $model->data[$model->alias][$field] = $this->_corrigirValor($value);
            }
        }
        return true;
    }

    public function _corrigirValor($value) {
        // verifica se tem virgula na string
        if (stripos($value, ',')) {
            // retira os pontos por nada.
            $value = str_replace('.', '', $value);
            // converte a virgula por ponto
            $value = str_replace(',', '.', $value);
        }
        // remove todas as letras e caracteres especiais da string.
        $value = preg_replace("/[^0-9.\s]/", "", $value);
        // converte em float
        return (float) trim($value);
    }

}
